<template>
  <div class="lb-exemption-pwd">
    <top-nav></top-nav>
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="pwdForm"
        :rules="pwdFormRules"
        ref="pwdForm"
        label-width="140px"
        class="pwd-form"
      >
        <el-form-item label="免审口令" prop="code">
          <el-input
            v-model="pwdForm.code"
            placeholder="请输入免审口令"
          ></el-input>
          <lb-tool-tips>{{ tips[1] }}</lb-tool-tips>
        </el-form-item>
        <el-form-item label="口令错误" prop="btn_code_err">
          <el-input
            v-model="pwdForm.btn_code_err"
            placeholder="请输入口令错误文案"
          ></el-input>
          <lb-tool-tips>{{ tips[2] }}</lb-tool-tips>
        </el-form-item>
        <el-form-item label="未输入口令" prop="btn_code_miss">
          <el-input
            v-model="pwdForm.btn_code_miss"
            placeholder="请输入未输入口令时的文案"
          ></el-input>
          <lb-tool-tips>{{ tips[3] }}</lb-tool-tips>
        </el-form-item>
        <el-form-item>
          <lb-button type="primary" @click="getAndSubmitFromInfo(1)">{{
            $t('action.submit')
          }}</lb-button>
          <!-- <lb-button @click="resetForm">{{$t('action.reset')}}</lb-button> -->
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      tips: {
        1: `设置了此选项后，用户在小程序端创建${this.$t('menu.BusinessCard')}时输入此正确免审口令则无需管理员在后台设置员工，将自动成为员工`,
        2: `用户在小程序端创建${this.$t('menu.BusinessCard')}时，免审口令输入错误提示文案`,
        3: `用户在小程序端创建${this.$t('menu.BusinessCard')}时，免审口令没有输入提示文案`
      },
      pwdForm: {
        action: '',
        code: '',
        btn_code_err: '',
        btn_code_miss: ''
      },
      pwdFormRules: {
        code: { type: 'string', message: '请输入免审口令', trigger: 'blur' },
        btn_code_err: { required: true, type: 'string', message: '请输入口令错误文案', trigger: 'blur' },
        btn_code_miss: { required: true, type: 'string', message: '请输入未输入口令时文案', trigger: 'blur' }
      }
    }
  },
  created () {
    this.getAndSubmitFromInfo(0)
  },
  methods: {
    /**
     * @method 回显表单信息、提交表单信息
     */
    getAndSubmitFromInfo (flag) {
      let queryForm = JSON.parse(JSON.stringify(this.pwdForm))
      queryForm.action = ''
      if (flag === 1) {
        this.$refs['pwdForm'].validate(valid => {
          if (valid) {
            queryForm.action = 'edit'
            this.handleAjaxForm(queryForm, flag)
          } else {
            this.$message.error(this.$t('tips.fillValidate'))
          }
        })
      } else {
        this.handleAjaxForm(queryForm, flag)
      }
    },
    handleAjaxForm (formInfo, flag) {
      this.$api.editExemptionPwd(formInfo).then(res => {
        if (res.code === 200) {
          if (flag !== 1) {
            this.pwdForm = { action: '', ...res.data }
          } else {
            this.$message.success(this.$t('tips.successSub'))
          }
        }
      })
    },
    /**
     * @method 重置表单
     */
    resetForm () {
      this.$refs['pwdForm'].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-exemption-pwd {
  width: 100%;
  .pwd-form {
    .el-input {
      width: 300px;
    }
  }
}
</style>
